package com.tanhua.api.mongo;

import com.tanhua.mongo.RecommendUser;
import com.tanhua.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    public RecommendUser queryWithMaxScore(Long userId) {
        Query query = new Query(Criteria.where("toUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"score"));
        query.skip(0).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageBeanVo findAllScore(Integer page, Integer pagesize, Long userId) {

        Query query = new Query(Criteria.where("toUserId").is(userId));
        query.skip( (page-1)*pagesize ).limit(pagesize);
        //总条数
        long count = mongoTemplate.count(query, RecommendUser.class);
        //页面分页展示的数据
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //封装
        PageBeanVo vo = new PageBeanVo();
        vo.setCounts(count);
        vo.setItems(list); //此时pageBean还是佳人的id集合
        return vo;
    }

    @Override
    public Long findScore(Long jrid, Long userId) {
        Query query = new Query(Criteria.where("userId").is(jrid).and("toUserId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //设置假数据
        if(recommendUser==null){
            recommendUser=new RecommendUser();
            recommendUser.setScore(99d);
        }
        return recommendUser.getScore().longValue();
    }
}
